package com.pm.business.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.pm.business.service.IBidProjectWorkflowService;
import com.pm.common.constant.HttpStatus;
import com.pm.common.core.page.PageDomain;
import com.pm.common.core.page.TableSupport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.business.domain.BidProjectProcess;
import com.pm.business.service.IBidProjectProcessService;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 项目投标执行Controller
 * 
 * @author zxl
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/business/bid_project_process")
public class BidProjectProcessController extends BaseController
{
    @Autowired
    private IBidProjectProcessService bidProjectProcessService;
    @Autowired
    private IBidProjectWorkflowService bidProjectWorkflowService;

    /**
     * 查询项目投标执行列表
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:list')")
    @GetMapping("/list")
    public TableDataInfo list(BidProjectProcess bidProjectProcess)
    {
        startPage();

        List<Long> projectIdList = bidProjectProcessService.selectActiveBidProjectId(bidProjectProcess);
        List<BidProjectProcess> result = new ArrayList<>();
        if(projectIdList!=null && !projectIdList.isEmpty()){
            result = bidProjectProcessService.selectBidProjectAndProcessList(projectIdList);
            List<BidProjectProcess> pointList = bidProjectProcessService.selectBidProjectAllPointList(projectIdList);
            if(result!=null && pointList!=null){
                Map<Long,String> map = new HashMap<Long,String>();
                pointList.forEach(item ->{
                    map.put(item.getProjectId(),item.getPointName());
                });
                Long maxId = bidProjectProcessService.selectMaxId();
                for (int i=0;i<result.size();i++){
                    result.get(i).setPointName(map.get(result.get(i).getProjectId()));
                    result.get(i).setProcessId(++maxId);
                }
            }
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(result);
        rspData.setTotal(new PageInfo(projectIdList).getTotal());
        return rspData;
        //return getDataTable(result);
    }

    /**
     * 查询项目投标执行详情列表
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:list')")
    @GetMapping("/process/list")
    public TableDataInfo processList(BidProjectProcess bidProjectProcess)
    {
        startPage();
        return getDataTable(bidProjectProcessService.selectBidProjectProcessList(bidProjectProcess));
    }

    /**
     * 导出项目投标执行列表
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:export')")
    @Log(title = "项目投标执行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BidProjectProcess bidProjectProcess)
    {
        List<BidProjectProcess> list = bidProjectProcessService.selectBidProjectProcessList(bidProjectProcess);
        ExcelUtil<BidProjectProcess> util = new ExcelUtil<BidProjectProcess>(BidProjectProcess.class);
        util.exportExcel(response, list, "项目投标执行数据");
    }

    /**
     * 获取项目投标执行详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:query')")
    @GetMapping(value = "/{processId}")
    public AjaxResult getInfo(@PathVariable("processId") Long processId)
    {
        return AjaxResult.success(bidProjectProcessService.selectBidProjectProcessByProcessId(processId));
    }
    @GetMapping(value = "/get_step_info/{projectId}")
    public AjaxResult getStepInfo(@PathVariable("projectId") Long projectId)
    {
        return AjaxResult.success(bidProjectWorkflowService.selectBidProjectProcessByProjectId(projectId));
    }

    /**
     * 新增项目投标执行
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:add')")
    @Log(title = "项目投标执行", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BidProjectProcess bidProjectProcess)
    {
        return toAjax(bidProjectProcessService.insertBidProjectProcess(bidProjectProcess));
    }


    @Log(title = "项目投标激活", businessType = BusinessType.INSERT)
    @PostMapping("/active")
    public AjaxResult activeBidProjectProcess(@RequestBody BidProjectProcess bidProjectProcess)
    {
        List<Long>condition = new ArrayList<>();
        condition.add(bidProjectProcess.getProjectId());

        List<BidProjectProcess> exists = bidProjectProcessService.selectBidProjectAllPointList(condition);
        if(exists!=null && exists.size()>0){
            return AjaxResult.error("项目已经进入招标进程");
        }
        return toAjax(bidProjectProcessService.activeBidProjectProcess(bidProjectProcess));
    }

    /**
     * 修改项目投标执行
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:edit')")
    @Log(title = "项目投标执行", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BidProjectProcess bidProjectProcess)
    {
        return toAjax(bidProjectProcessService.updateBidProjectProcess(bidProjectProcess));
    }

    /**
     * 删除项目投标执行
     */
    @PreAuthorize("@ss.hasPermi('business:bid_project_process:remove')")
    @Log(title = "项目投标执行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{processIds}")
    public AjaxResult remove(@PathVariable Long[] processIds)
    {
        return toAjax(bidProjectProcessService.deleteBidProjectProcessByProcessIds(processIds));
    }
}
